**************************************************
**** Immigration and International Law
**** Regression Analysis
**** Replication .do file (2 of 3)
**** Margaret E. Peters 
**** July 2018


version 13.1
set more off

********************************************
***Put all the datasets together
********************************************


***Create dataset to drop years under a treaty & create years since last treaty
use "bilateral_for_ddd_analysis_2017_07.dta", clear

gen tr_st_year=year
label var tr_st_year "Treaty Start Year"

sort dyadid year
*by dyadid: gen l_year=year[_n-1]

by dyadid: gen trcase=_n


gen tr_end_year=year_ended if year_ended!=.
label var tr_end_year "Treaty End Year"

*countries that ended treaties with first oil crises
replace tr_end_year=1974 if ccode1==210
replace tr_end_year=1974 if ccode1==211
replace tr_end_year=1974 if ccode1==212
replace tr_end_year=1974 if ccode1==220
replace tr_end_year=1974 if ccode1==225
replace tr_end_year=1973 if ccode1==260
replace tr_end_year=1973 if ccode1==305
replace tr_end_year=1974 if ccode1==380
replace tr_end_year=1974 if ccode1==385
replace tr_end_year=1974 if ccode1==390
replace tr_end_year=1973 if ccode1==900



keep ccode1 ccode2 dyadid tr_st_year  tr_end_year l_year trcase
reshape wide l_year tr_st_year tr_end_year, i(dyadid) j(trcase)

save "bilateral_for_yrsincety_dropyears.dta", replace



*import polity data first to increase years
import delimited "polity_dyads_2017_07_17.csv", clear

renvars ccode_1 ccode_2 year_1 polity2_1 xconst_1 year_2 polity2_2 xconst_2 \ ccode1 ccode2 year polity1 xconst1 year2 polity2 xconst2
drop year2

save "polity_ct1_ct2.dta", replace
renvars ccode1 polity1 xconst1, postf(temp)
renvars ccode2 polity2 xconst2, postd(1)
renvars ccode polity xconst, postf(1)
renvars ccode1temp polity1temp xconst1temp, postd(5)
renvars ccode polity xconst, postf(2)
save "polity_ct2_ct1.dta", replace

use "polity_ct1_ct2.dta", clear
append using "polity_ct2_ct1.dta"
save "polity_dyads_2017_07.dta", replace

*import Distance variables from NewGene
import delimited "dist_dyads_2017_07_17", clear
drop v6 conttype
save "dist_ct1_ct2.dta", replace
renvars ccode1, postf(temp)
renvars ccode2, postd(1)
renvars ccode, postf(1)
renvars ccode1temp, postd(5)
renvars ccode, postf(2)
save "dist_ct2_ct1.dta", replace

use "dist_ct1_ct2.dta", clear
append using "dist_ct2_ct1.dta"

*fix that first year of dyad often as mindist==0
gen dyadid=ccode1+ccode2/1000
sort dyadid year
by dyadid, sort: gen lead_mindistance=mindistance[_n+1]
by dyadid, sort: gen lag_mindistance=mindistance[_n-1]
replace mindistance=lead_mindistance if mindistance==0 & lag_mindistance==.
drop dyadid lead_mindistance lag_mindistance

save "dist_dyads_2017_07.dta", replace


*import IPE variables from NewGene to add to directed dyad dataset

import delimited "ipe_dyads_2017_07_17", clear

renvars ccode_1 ccode_2 year_1 \ ccode1 ccode2 year

save "ipe_dyads_ct1_ct2.dta", replace
drop year_2

renvars ccode1  tpop_mc_1 upop_mc_1 gdp_wb_1 gdppc_wb_1 growth_wb_1 migrant_per_wb_1 ////
migrant_tot_wb_1 pop_wb_1 pop_urb_wb_1 remittances_wb_1 reserves_im_wb_1 workingpop_wb_1 polity_p4_1 xconst_p4_1 ////
tyr_long_bl_1 yr_sch_25999_bl_1 tyr_long_int_bl_1 yr_sch_25999_int_bl_1, postf(temp)

renvars ccode2  tpop_mc_2 upop_mc_2 gdp_wb_2 gdppc_wb_2 growth_wb_2 migrant_per_wb_2 ////
migrant_tot_wb_2 pop_wb_2 pop_urb_wb_2 remittances_wb_2 reserves_im_wb_2 workingpop_wb_2 polity_p4_2 xconst_p4_2 ////
tyr_long_bl_2 yr_sch_25999_bl_2 tyr_long_int_bl_2 yr_sch_25999_int_bl_2, postd(1)

renvars ccode tpop_mc_ upop_mc_ gdp_wb_ gdppc_wb_ growth_wb_ migrant_per_wb_ ////
migrant_tot_wb_ pop_wb_ pop_urb_wb_ remittances_wb_ reserves_im_wb_ workingpop_wb_ polity_p4_ xconst_p4_ ////
tyr_long_bl_ yr_sch_25999_bl_ tyr_long_int_bl_ yr_sch_25999_int_bl_, postf(1)

renvars ccode1temp tpop_mc_1temp upop_mc_1temp gdp_wb_1temp gdppc_wb_1temp growth_wb_1temp migrant_per_wb_1temp ////
migrant_tot_wb_1temp pop_wb_1temp pop_urb_wb_1temp remittances_wb_1temp reserves_im_wb_1temp workingpop_wb_1temp polity_p4_1temp xconst_p4_1temp ////
tyr_long_bl_1temp yr_sch_25999_bl_1temp tyr_long_int_bl_1temp yr_sch_25999_int_bl_1temp, postd(5)

renvars ccode  tpop_mc_ upop_mc_ gdp_wb_ gdppc_wb_ growth_wb_ migrant_per_wb_ ////
migrant_tot_wb_ pop_wb_ pop_urb_wb_ remittances_wb_ reserves_im_wb_ workingpop_wb_ polity_p4_ xconst_p4_ ////
tyr_long_bl_ yr_sch_25999_bl_ tyr_long_int_bl_ yr_sch_25999_int_bl_, postf(2)
save "ipe_dyads_ct2_ct1.dta", replace

use "ipe_dyads_ct1_ct2.dta", clear
append using "ipe_dyads_ct2_ct1.dta"
sort ccode1 ccode2 year
duplicates list ccode1 ccode2 year

save "ipe_dyads_2017_07_17.dta", replace

*import Leblang Migration variables from NewGene to add to directed dyad dataset
import delimited "migdata_dyads_2017_07_17", clear /*already DDD data */
save "migdata_dyads_2017_07_17", replace





**Directed Dyad from Classic Eugene
import delimited "directed_dyads_2017_07_17.csv", clear
gen dyadid=ccode1+ccode2/1000
drop if year==.

merge m:m ccode1 ccode2 year using "polity_dyads_2017_07"
drop _merge

merge m:m ccode1 ccode2 year using "ipe_dyads_2017_07_17"
drop _merge
duplicates list ccode1 ccode2 year


merge m:m ccode1 ccode2 year using "migdata_dyads_2017_07_17"
drop _merge
duplicates list ccode1 ccode2 year

merge m:m ccode1 ccode2 year using "dist_dyads_2017_07.dta"
drop _merge
duplicates list ccode1 ccode2 year

*add additional years of distance
sort ccode1 ccode2 year

foreach var in ccdistance mindistance {
by ccode1 ccode2: gen l1_`var'=`var'[_n-1]
by ccode1 ccode2: gen l2_`var'=`var'[_n-2]
by ccode1 ccode2: gen l3_`var'=`var'[_n-3]
by ccode1 ccode2: gen l4_`var'=`var'[_n-4]
by ccode1 ccode2: gen l5_`var'=`var'[_n-5]
by ccode1 ccode2: gen l6_`var'=`var'[_n-6]
by ccode1 ccode2: gen l7_`var'=`var'[_n-7]
replace `var'=l1_`var' if year==2009
replace `var'=l2_`var' if year==2010
replace `var'=l3_`var' if year==2011
replace `var'=l4_`var' if year==2012
replace `var'=l5_`var' if year==2013
replace `var'=l6_`var' if year==2014
replace `var'=l7_`var' if year==2015
drop l1_`var' l2_`var' l3_`var' l4_`var' l5_`var' l6_`var' l7_`var' 
}

foreach var in ccdistance mindistance {
by ccode1 ccode2: gen l1_`var'=`var'[_n+1]
by ccode1 ccode2: gen l2_`var'=`var'[_n+2]
by ccode1 ccode2: gen l3_`var'=`var'[_n+3]
by ccode1 ccode2: gen l4_`var'=`var'[_n+4]
by ccode1 ccode2: gen l5_`var'=`var'[_n+5]
by ccode1 ccode2: gen l6_`var'=`var'[_n+6]
by ccode1 ccode2: gen l7_`var'=`var'[_n+7]
by ccode1 ccode2: gen l8_`var'=`var'[_n+8]
by ccode1 ccode2: gen l9_`var'=`var'[_n+9]
by ccode1 ccode2: gen l10_`var'=`var'[_n+10]
by ccode1 ccode2: gen l11_`var'=`var'[_n+11]
by ccode1 ccode2: gen l12_`var'=`var'[_n+12]
by ccode1 ccode2: gen l13_`var'=`var'[_n+13]
by ccode1 ccode2: gen l14_`var'=`var'[_n+14]
by ccode1 ccode2: gen l15_`var'=`var'[_n+15]
by ccode1 ccode2: gen l16_`var'=`var'[_n+16]
by ccode1 ccode2: gen l17_`var'=`var'[_n+17]
by ccode1 ccode2: gen l18_`var'=`var'[_n+18]
by ccode1 ccode2: gen l19_`var'=`var'[_n+19]
by ccode1 ccode2: gen l20_`var'=`var'[_n+20]
by ccode1 ccode2: gen l21_`var'=`var'[_n+21]
by ccode1 ccode2: gen l22_`var'=`var'[_n+22]
by ccode1 ccode2: gen l23_`var'=`var'[_n+23]
by ccode1 ccode2: gen l24_`var'=`var'[_n+24]
by ccode1 ccode2: gen l25_`var'=`var'[_n+25]
by ccode1 ccode2: gen l26_`var'=`var'[_n+26]
by ccode1 ccode2: gen l27_`var'=`var'[_n+27]
by ccode1 ccode2: gen l28_`var'=`var'[_n+28]
by ccode1 ccode2: gen l29_`var'=`var'[_n+29]
by ccode1 ccode2: gen l30_`var'=`var'[_n+30]
replace `var'=l1_`var' if `var'==.
replace `var'=l1_`var' if `var'==.
replace `var'=l2_`var' if `var'==.
replace `var'=l3_`var' if `var'==.
replace `var'=l4_`var' if `var'==.
replace `var'=l5_`var' if `var'==.
replace `var'=l6_`var' if `var'==.
replace `var'=l7_`var' if `var'==.
replace `var'=l8_`var' if `var'==.
replace `var'=l9_`var' if `var'==.
replace `var'=l10_`var' if `var'==.
replace `var'=l11_`var' if `var'==.
replace `var'=l12_`var' if `var'==.
replace `var'=l13_`var' if `var'==.
replace `var'=l14_`var' if `var'==.
replace `var'=l15_`var' if `var'==.
replace `var'=l16_`var' if `var'==.
replace `var'=l17_`var' if `var'==.
replace `var'=l18_`var' if `var'==.
replace `var'=l19_`var' if `var'==.
replace `var'=l20_`var' if `var'==.
replace `var'=l21_`var' if `var'==.
replace `var'=l22_`var' if `var'==.
replace `var'=l23_`var' if `var'==.
replace `var'=l24_`var' if `var'==.
replace `var'=l25_`var' if `var'==.
replace `var'=l26_`var' if `var'==.
replace `var'=l27_`var' if `var'==.
replace `var'=l28_`var' if `var'==.
replace `var'=l29_`var' if `var'==.
replace `var'=l30_`var' if `var'==.
}

drop l1_ccdistance l2_ccdistance l3_ccdistance l4_ccdistance l5_ccdistance l6_ccdistance l7_ccdistance l8_ccdistance l9_ccdistance l10_ccdistance l11_ccdistance l12_ccdistance l13_ccdistance l14_ccdistance l15_ccdistance l16_ccdistance l17_ccdistance l18_ccdistance l19_ccdistance l20_ccdistance l21_ccdistance l22_ccdistance l23_ccdistance l24_ccdistance l25_ccdistance l26_ccdistance l27_ccdistance l28_ccdistance l29_ccdistance l30_ccdistance l1_mindistance l2_mindistance l3_mindistance l4_mindistance l5_mindistance l6_mindistance l7_mindistance l8_mindistance l9_mindistance l10_mindistance l11_mindistance l12_mindistance l13_mindistance l14_mindistance l15_mindistance l16_mindistance l17_mindistance l18_mindistance l19_mindistance l20_mindistance l21_mindistance l22_mindistance l23_mindistance l24_mindistance l25_mindistance l26_mindistance l27_mindistance l28_mindistance l29_mindistance l30_mindistance

****Add in additional datasets
*immigration policy
merge m:m ccode1 year using "immipolfortreaty.dta"
duplicates list ccode1 ccode2 year
drop if year<1946
drop if year==.
drop _merge


*Additional WDI variables
merge m:m ccode1 year using "wdi_indicators_ct1.dta"
duplicates list ccode1 ccode2 year
drop _merge


merge m:m ccode2 year using "wdi_indicators_ct2.dta"
duplicates list ccode1 ccode2 year
drop _merge
drop if year==.

*Economic treaties
merge m:m ccode1 ccode2 year using "econ_wti_COW.dta"
duplicates drop ccode1 ccode2 year , force
drop if ccode1==ccode2
drop _merge



merge m:m ccode1 ccode2 year using "econ_wti_COW_ct2_ct1.dta"
duplicates drop ccode1 ccode2 year , force
drop if ccode1==ccode2
drop _merge

drop if year<1946
drop if year==.

*Data from the Conference Board
merge m:m ccode1 year using "confboardcow1.dta"
duplicates list ccode1 ccode2 year
drop _merge


merge m:m ccode2 year using "confboardcow2.dta"
duplicates list ccode1 ccode2 year
drop _merge


drop if year<1946
drop if year==.


*Veto Players
merge m:m ccode1 year using "polconfortreaty_2017.dta"
duplicates list ccode1 ccode2 year
drop _merge

drop if year<1946
drop if year==.


*Colonies
merge m:m ccode1 using "colony1.dta"
duplicates list ccode1 ccode2 year
drop _merge

drop if year<1946
drop if year==.



merge m:m ccode2  using "colony2.dta"
duplicates list ccode1 ccode2 year
drop _merge

drop if year<1946
drop if year==.


****Add treaty datasets 
**main treaty dataset
merge m:m ccode1 ccode2 year using "bilateral_for_ddd_analysis_2017_07"

list ccode1 ccode2 year if _merge==2

duplicates list ccode1 ccode2 year
drop _merge

**dataset to create years since treaty and years under treaty
merge m:m ccode1 ccode2 using "bilateral_for_yrsincety_dropyears"
drop _merge


****Stop and save dataset so far
save "treaty_data_merged.dta", replace

***************************************************************
***** Create new variables
**************************************************************
cd "/Users/mp749/Dropbox/Maggie's FIles/Immigration Treaty Paper/"
use "treaty_data_merged.dta", clear
sort ccode1 ccode2 year


*Create Years Since Treaty Variable
gen yrsincetr=year-l_year2 if l_year2!=. & year>l_year2
replace yrsincetr=year-l_year3 if l_year3!=. & year>l_year3
replace yrsincetr=year-l_year4 if l_year4!=. & year>l_year4
replace yrsincetr=year-l_year5 if l_year5!=. & year>l_year5
replace yrsincetr=year-l_year6 if l_year6!=. & year>l_year6
replace yrsincetr=year-l_year7 if l_year7!=. & year>l_year7
replace yrsincetr=year-l_year8 if l_year8!=. & year>l_year8
replace yrsincetr=year-l_year9 if l_year9!=. & year>l_year9
replace yrsincetr=year-l_year10 if l_year10!=. & year>l_year10
replace yrsincetr=year-l_year11 if l_year11!=. & year>l_year11
replace yrsincetr=year-l_year12 if l_year12!=. & year>l_year12
replace yrsincetr=year-l_year13 if l_year13!=. & year>l_year13
replace yrsincetr=year-1946 if l_year2==. & l_year3==. & l_year4==. & l_year5==. & l_year6==. & l_year7==. & l_year8==. & l_year9==. & l_year10==. ////
& l_year11==. & l_year12==. & l_year13==. 
replace yrsincetr=year-1946 if yrsincetr==.
label var yrsincetr "Years Since Treaty"

*B&K Splines
spbase yrsince, gen(spl) nknots(3)
label variable spl1 "Spline 1"
label variable spl2 "Spline 2"
label variable spl3 "Spline 3"

*C&S cubic
gen yrsincetr2=yrsincetr^2
gen yrsincetr3=yrsincetr^3
label var yrsincetr2 "Years Since Treaty Squared"
label var yrsincetr3 "Years Since Treaty Cubed"

****Create Variables to Drop Years Under Treaty
gen yrundertr=0
replace yrundertr=0 if year==tr_st_year1
replace yrundertr=1 if year>tr_st_year1 & year<tr_end_year1

replace yrundertr=0 if year==tr_st_year2
replace yrundertr=1 if year>tr_st_year2 & year<tr_end_year2

replace yrundertr=0 if year==tr_st_year3
replace yrundertr=1 if year>tr_st_year3 & year<tr_end_year3

replace yrundertr=0 if year==tr_st_year4
replace yrundertr=1 if year>tr_st_year4 & year<tr_end_year4

replace yrundertr=0 if year==tr_st_year5
replace yrundertr=1 if year>tr_st_year5 & year<tr_end_year5

replace yrundertr=0 if year==tr_st_year6
replace yrundertr=1 if year>tr_st_year6 & year<tr_end_year6

replace yrundertr=0 if year==tr_st_year7
replace yrundertr=1 if year>tr_st_year7 & year<tr_end_year7

replace yrundertr=0 if year==tr_st_year8
replace yrundertr=1 if year>tr_st_year8 & year<tr_end_year8

replace yrundertr=0 if year==tr_st_year9
replace yrundertr=1 if year>tr_st_year9 & year<tr_end_year9

replace yrundertr=0 if year==tr_st_year10
replace yrundertr=1 if year>tr_st_year10 & year<tr_end_year10

replace yrundertr=0 if year==tr_st_year11
replace yrundertr=1 if year>tr_st_year11 & year<tr_end_year11

replace yrundertr=0 if year==tr_st_year12
replace yrundertr=1 if year>tr_st_year12 & year<tr_end_year12


replace yrundertr=0 if year==tr_st_year13
replace yrundertr=1 if year>tr_st_year13 & year<tr_end_year13

label var yrundertr "Years Under Treaty"

*EU members 
gen eumem1=0
gen eumem2=0
replace eumem1=1 if ccode1==305 & year>=1995
replace eumem2=1 if ccode2==305 & year>=1995
replace eumem1=1 if ccode1==211 & year>=1968 
replace eumem2=1 if ccode2==211 & year>=1968
replace eumem1=1 if ccode1==390 & year>=1973
replace eumem2=1 if ccode2==390 & year>=1973
replace eumem1=1 if ccode1==375 & year>=1995
replace eumem2=1 if ccode2==375 & year>=1995
replace eumem1=1 if ccode1==220 & year>=1968
replace eumem2=1 if ccode2==220 & year>=1968
replace eumem1=1 if ccode1==260 & year>=1968
replace eumem1=1 if ccode1==255 & year>=1968
replace eumem2=1 if ccode2==260 & year>=1968
replace eumem2=1 if ccode2==255 & year>=1968
replace eumem1=1 if ccode1==350 & year>=1981
replace eumem2=1 if ccode2==350 & year>=1981
replace eumem1=1 if ccode1==205 & year>=1973
replace eumem2=1 if ccode2==205 & year>=1973
replace eumem1=1 if ccode1==325 & year>=1968
replace eumem2=1 if ccode2==325 & year>=1968
replace eumem1=1 if ccode1==212 & year>=1968
replace eumem2=1 if ccode2==212 & year>=1968
replace eumem1=1 if ccode1==210 & year>=1968
replace eumem2=1 if ccode2==210 & year>=1968
replace eumem1=1 if ccode1==235 & year>=1986
replace eumem2=1 if ccode2==235 & year>=1986
replace eumem1=1 if ccode1==230 & year>=1986
replace eumem2=1 if ccode2==230 & year>=1986
replace eumem1=1 if ccode1==380 & year>=1995
replace eumem2=1 if ccode2==380 & year>=1995
replace eumem1=1 if ccode1==200 & year>=1973
replace eumem1=1 if ccode2==200 & year>=1973

gen botheumem=0
replace botheumem=1 if eumem1==1 & eumem2==1 

label variable eumem1 "RS EU Member" 
label variable eumem2 "SS EU Member"
label variable botheumem "Both states EU members"

replace yrundertr=1 if botheumem==1 & year>=1992

*****Create variables to mark which observations to drop
gen origin=treaty2
replace origin=0 if treaty2==.
label variable origin "Originating treaties"

gen amending=0
replace amending=1 if treaty1==1 & treaty2!=1
replace amending=0 if amending==.
label variable amending "Amending treaties"

gen yrunderorigin=0
replace yrunderorigin=1 if yrundertr==1
replace yrunderorigin=1 if amending==1
label variable yrunderorigin "Year under originating treaties"

gen alltreaties=treaty1
replace alltreaties=0 if treaty1==.

gen yrunderboth=0
replace yrunderboth=1 if yrundertr==1
label variable yrunderboth "Year under all treaties"

****Other variables
replace dyadid=ccode1+ccode2/1000

** Competition between regional receiving states
gen region1=0 if ccode1<100
replace region1=1 if ccode1>=100 & ccode1<200
replace region1=2 if ccode1>=200 & ccode1<400
replace region1=3 if ccode1>=400 & ccode1<600
replace region1=4 if ccode1>=600 & ccode1<700
replace region1=5 if ccode1>=700 & ccode1<900
replace region1=6 if ccode1>=900 

sort region1 year
by region1 year, sort: egen sumtreaty1=sum(treaty1)

gen sumtreaty1_1=sumtreaty1-treaty1

gen regtreaty1=0
replace regtreaty1=1 if sumtreaty1_1>0

sort dyad year
by dyad: gen lag1_1=regtreaty1[_n-5]
by dyad: gen lag2_1=regtreaty1[_n-4]
by dyad: gen lag3_1=regtreaty1[_n-3]
by dyad: gen lag4_1=regtreaty1[_n-2]
by dyad: gen lag5_1=regtreaty1[_n-1]
 

gen regtreaty1_5=0
replace regtreaty1_5=0
replace regtreaty1_5=1 if lag1_1==1 | lag2_1==1 | lag3_1==1 | lag4_1==1 | lag5_1==1
label variable regtreaty1_5 "Regional competition"

***Competition for immigrants from one sending state
sort ccode2 year
by ccode2 year, sort: egen sumtreaty2=sum(treaty1)

gen sumtreaty2_2=sumtreaty2-treaty1

gen comp=0
replace comp=0
replace comp=1 if sumtreaty2_2>0

sort dyad year
by dyad: gen lag1_2=comp[_n-5]
by dyad: gen lag2_2=comp[_n-4]
by dyad: gen lag3_2=comp[_n-3]
by dyad: gen lag4_2=comp[_n-2]
by dyad: gen lag5_2=comp[_n-1]

gen comp_5=0
replace comp_5=0
replace comp_5=1 if lag1_2==1 | lag2_2==1 | lag3_2==1 | lag4_2==1 | lag5_2==1
label variable comp_5 "SS competition"

***Generate Remote
sort ccode1

by ccode1: egen meandist=mean(mindistance)
summarize meandist, detail
gen remote75=0
replace remote75=1 if meandist>=8412.854 

label var remote75 "Remote (75%)"

gen remote90=0
replace remote90=1 if meandist>=9601.091  

label var remote90 "Remote (90%)"

gen remote50=0
replace remote50=1 if meandist>=6056.483 

label var remote50 "Remote (50%)"


***lagged immi policy
gen dyad=ccode1+ccode2/1000

sort dyad year
by dyad: gen immipol=factor1[_n-1]
label variable immipol "Immi. policy RS (1 yr lag)"


***lagged unemployment
by dyad: gen lag_unempilo2=unempilo2[_n-1]
label variable lag_unempilo2 "Unemployment SS (1 yr lag)"

by dyad: gen lag_unempilo1=unempilo1[_n-1]
label variable lag_unempilo1 "Unemployment RS (1 yr lag)"

***lagged employment from conf board
by dyad: gen lag_ted_empper2=ted_empper2[_n-1]
label variable lag_ted_empper2 "Employment SS (1 yr lag)"

by dyad: gen lag_ted_empper1=ted_empper1[_n-1]
label variable lag_ted_empper1 "Employment RS (1 yr lag)"


***Lagged productivity from conf board
*basic productivity
foreach var in ted_outemp1 ted_outhour1 ted_outemp2 ted_outhour2 {
gen ln_`var'=ln(`var')
by dyad: gen lag_ln_`var'=ln_`var'[_n-1]
}

label var lag_ln_ted_outemp1 "Output per employee RS (1 yr lag)"
label var lag_ln_ted_outemp2 "Output per employee SS (1 yr lag)"

label var lag_ln_ted_outhour1 "Output per hour RS (1 yr lag)"
label var lag_ln_ted_outhour2 "Output per hour SS (1 yr lag)"


*productivity growth 
foreach var in ted_outempgrow1 ted_outhourgrow1 ted_outempgrow2 ted_outhourgrow2 {
by dyad: gen lag_`var'=`var'[_n-1]
}

label var lag_ted_outempgrow1 "Output per employee Growth RS (1 yr lag)"
label var lag_ted_outempgrow2 "Output per employee Growth SS (1 yr lag)"

label var lag_ted_outhourgrow1 "Output per hour Growth RS (1 yr lag)"
label var lag_ted_outhourgrow2 "Output per hour Growth SS (1 yr lag)"






****Signing another economic treaty
replace econaidtrsigned=0 if econaidtrsigned==.

sort dyad year
by dyad: gen lag_econ=econaidtrsigned[_n-1]
by dyad: gen lead_econ=econaidtrsigned[_n+1]

gen econpm1=0
replace econpm1=1 if lag_econ==1 
replace econpm1=1 if econaidtrsigned==1
replace econpm1=1 if lead_econ==1
label variable econpm1 "Economic or aid treaty signed +/- 1 year"


**** Regime type and democratization
gen autoc_rs=0
replace autoc_rs=1 if polity_p4_1<-6
label variable autoc_rs "RS autocracy"

gen anoc_rs=0 
replace anoc_rs=1 if polity_p4_1>=-6 & polity_p4_1<=6
label variable anoc_rs "RS anocracy"

gen democ_rs=0
replace democ_rs=1 if polity_p4_1>6
label variable democ_rs "RS democracy"

sort dyad year
by dyad: gen autoc_rs1=autoc_rs[_n-1]
by dyad: gen autoc_rs2=autoc_rs[_n-2]
by dyad: gen autoc_rs3=autoc_rs[_n-3]
by dyad: gen autoc_rs4=autoc_rs[_n-4]
by dyad: gen autoc_rs5=autoc_rs[_n-5]

by dyad: gen anoc_rs1=anoc_rs[_n-1]
by dyad: gen anoc_rs2=anoc_rs[_n-2]
by dyad: gen anoc_rs3=anoc_rs[_n-3]
by dyad: gen anoc_rs4=anoc_rs[_n-4]
by dyad: gen anoc_rs5=anoc_rs[_n-5]

by dyad: gen democ_rs1=democ_rs[_n-1]
by dyad: gen democ_rs2=democ_rs[_n-2]
by dyad: gen democ_rs3=democ_rs[_n-3]
by dyad: gen democ_rs4=democ_rs[_n-4]
by dyad: gen democ_rs5=democ_rs[_n-5]

gen democratizing_rs=0
replace democratizing_rs=1 if autoc_rs1==1 & democ_rs==1
replace democratizing_rs=1 if autoc_rs2==1 & democ_rs==1
replace democratizing_rs=1 if autoc_rs3==1 & democ_rs==1
replace democratizing_rs=1 if autoc_rs4==1 & democ_rs==1
replace democratizing_rs=1 if autoc_rs5==1 & democ_rs==1

replace democratizing_rs=1 if anoc_rs1==1 & democ_rs==1
replace democratizing_rs=1 if anoc_rs2==1 & democ_rs==1
replace democratizing_rs=1 if anoc_rs3==1 & democ_rs==1
replace democratizing_rs=1 if anoc_rs4==1 & democ_rs==1
replace democratizing_rs=1 if anoc_rs5==1 & democ_rs==1

replace democratizing_rs=1 if autoc_rs1==1 & anoc_rs==1
replace democratizing_rs=1 if autoc_rs2==1 & anoc_rs==1
replace democratizing_rs=1 if autoc_rs3==1 & anoc_rs==1
replace democratizing_rs=1 if autoc_rs4==1 & anoc_rs==1
replace democratizing_rs=1 if autoc_rs5==1 & anoc_rs==1
label variable democratizing_rs "RS democratizing"

gen autocratizing_rs=0
replace autocratizing_rs=1 if democ_rs1==1 & autoc_rs==1
replace autocratizing_rs=1 if democ_rs2==1 & autoc_rs==1
replace autocratizing_rs=1 if democ_rs3==1 & autoc_rs==1
replace autocratizing_rs=1 if democ_rs4==1 & autoc_rs==1
replace autocratizing_rs=1 if democ_rs5==1 & autoc_rs==1

replace autocratizing_rs=1 if anoc_rs1==1 & autoc_rs==1
replace autocratizing_rs=1 if anoc_rs2==1 & autoc_rs==1
replace autocratizing_rs=1 if anoc_rs3==1 & autoc_rs==1
replace autocratizing_rs=1 if anoc_rs4==1 & autoc_rs==1
replace autocratizing_rs=1 if anoc_rs5==1 & autoc_rs==1

replace autocratizing_rs=1 if democ_rs1==1 & anoc_rs==1
replace autocratizing_rs=1 if democ_rs2==1 & anoc_rs==1
replace autocratizing_rs=1 if democ_rs3==1 & anoc_rs==1
replace autocratizing_rs=1 if democ_rs4==1 & anoc_rs==1
replace autocratizing_rs=1 if democ_rs5==1 & anoc_rs==1
label variable autocratizing_rs "RS autocratizing"


***veto players times democracy
gen polcondem=polconii*democ_rs
label variable polcondem "Veto players*Democracy"

***Same colonial system

gen samesys=0
replace samesys=1 if ausys1==1 & ausys2==1
replace samesys=1 if besys1==1 & besys2==1
replace samesys=1 if chsys1==1 & chsys2==1
replace samesys=1 if desys1==1 & desys2==1
replace samesys=1 if frsys1==1 & frsys2==1
replace samesys=1 if gesys1==1 & gesys2==1
replace samesys=1 if insys1==1 & insys2==1
replace samesys=1 if itsys1==1 & itsys2==1
replace samesys=1 if jasys1==1 & jasys2==1
replace samesys=1 if nesys1==1 & nesys2==1
replace samesys=1 if nzsys1==1 & nzsys2==1
replace samesys=1 if pasys1==1 & pasys2==1
replace samesys=1 if posys1==1 & posys2==1
replace samesys=1 if rusys1==1 & rusys2==1
replace samesys=1 if sasys1==1 & sasys2==1
replace samesys=1 if spsys1==1 & spsys2==1
replace samesys=1 if tusys1==1 & tusys2==1
replace samesys=1 if uksys1==1 & uksys2==1
replace samesys=1 if ussys1==1 & ussys2==1
replace samesys=1 if ussrsys1==1 & ussrsys2==1



***English-speaking countries
gen english=0
replace english=1 if ccode1==58  | ccode1==31 | ccode1==53 | ccode1==80 | ccode1==571 | ccode1==471 | ccode1==20 | ccode1==54 | ccode1==531 | ccode1==950 | ////
ccode1==420 | ccode1==452 | ccode1==55 | ccode1==110 | ccode1==711 | ccode1==750 | ccode1==205 | ccode1==51 | ccode1==501 | ccode1==946 | ccode1==570 | ////
ccode1==450 | ccode1==553 | ccode1==338 | ccode1==983 | ccode1==590 | ccode1==987 | ccode1==565 | ccode1==970 | ccode1==920 | ccode1==475 | ccode1==770 |  ////
ccode1==986 | ccode1==910 | ccode1==840 | ccode1==517| ccode1==60 | ccode1==56 | ccode1==57 | ccode1==990 | ccode1==591 | ccode1==451 | ccode1==830 | ////
ccode1==560 | ccode1==625 | ccode1==572 | ccode1==510 | ccode1==947 | ccode1==955 | ccode1==52 | ccode1==500 | ccode1==935 | ccode1==551 | ccode1==552 |  ////
ccode1==900 | ccode1==2 | ccode1==200

label var english "English Speaking"

***French-speaking countries
gen french=0
replace french=1 if ccode1==490 | ccode1==220 | ccode1==211 | ccode1==434 | ccode1==439 | ccode1==516 | ccode1==471 | ccode1==482 | ccode1==522 | ////
ccode1==411 | ccode1==481 | ccode1==438 | ccode1==411 | ccode1==483 | ccode1==581 | ccode1==41 | ccode1==437 | ccode1==212 | ccode1==580 | ccode1==432 |  ////
ccode1==221 | ccode1==436 | ccode1==484 | ccode1==433 | ccode1==591 | ccode1==225 | ccode1==461 | ccode1==935

label var french "French Speaking"

***Save data
save "treaty_dataset_replication.dta", replace

********************************************************
*** Regressions
********************************************************

use  "treaty_dataset_replication.dta", clear
duplicates drop dyadid year, force
tsset dyadid year

gen mindistance_1000=mindistance/1000

global controls econaidtrsigned anoc_rs democ_rs xconst_p4_1 democratizing_rs  polconiii polcondem samesys mindistance_1000 
global controls2 econaidtrsigned samesys mindistance_1000 


gen ted_gdp2_2=ted_gdp2^2
gen ted_ln_gdp1=ln(ted_gdp1)
gen ted_ln_gdp2=ln(ted_gdp2)

gen ted_ln_gdppc1=ln(ted_gdppc1)
gen ted_ln_gdppc2=ln(ted_gdppc2)

global wealth ted_ln_gdp1  ted_ln_gdp2  
global wealth2 ted_ln_gdppc1  ted_ln_gdppc2  


label var lag_ted_empper1 "Reserve Pool of Labor (RS; 1yr lag)"
label var lag_ln_ted_outhour1 "Productivity (RS; logged; 1yr lag)"
label var remote75 "Remote (RS)"
label var yr_sch_25999_int_bl_2 "Years of Schooling (SS; interpolated)"
label var ted_ln_gdp1  "GDP (RS; logged)"
label var ted_ln_gdp2 "GDP (SS; logged)"  
label var ted_ln_gdppc1  "GDP per capita (RS; logged)"
label var ted_ln_gdppc2 "GDP per capita (SS; logged)"  
label var econaidtrsigned "Econ/Aid treaty "
label define tr 0 "Econ/Aid treaty " 1 "Econ/Aid treaty "
label values econaidtrsigned tr
label var anoc_rs "Anocracy (RS)"
label define anoc 0 "Anocracy (RS)" 1 "Anocracy (RS)"
label values anoc_rs anoc
label var democ_rs "Democracy (RS)"
label define democ 0 "Democracy (RS)" 1 "Democracy (RS)"
label values democ_rs democ
label var xconst_p4_1 "Exec Constraints RS"
label var democratizing_rs  "Democratizing (RS)"
label define deming 0 "Democratizing (RS)" 1 "Democratizing (RS)"
label values democratizing_rs deming
label var polconiii "Veto players"
label var polcondem "Veto players*Democracy"
label var mindistance_1000 "Minimum Distance"
label var samesys "Same colonial system"
label define sames 0 "Same colonial system" 1 "Same colonial system"
label values samesys sames
label var ted_empgrow2 "Employment Growth (SS)"
label var lag_unempilo2 "Unemployment (SS; lagged 1 year)"
gen empgrow_ed=ted_empgrow2*yr_sch_25999_int_bl_2
label var empgrow_ed "Employment Growth*Education (SS)" 

**Table 1
set more off
eststo clear
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
esttab using table1.tex,label wide b(2) se(2)  star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
title("Logistic regressions testing the hypotheses" \label{tab:allorigin}) nogaps booktabs replace ///
compress order(lag_ted_empper1 remote75 lag_ln_ted_outhour1 yr_sch_25999_int_bl_2 ted_empgrow2 ted_ln_gdp1 ted_ln_gdp2 ////
econaidtrsigned anoc_rs democ_rs democratizing_rs    polconiii polcondem xconst_p4_1 samesys mindistance_1000 yrsincetr yrsincetr2 yrsincetr3)
  
  

**Marginal Effects
set more off
logit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)

gen sample=0
replace sample=1 if e(sample)

summarize lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth $controls if sample==1, detail

*employment
set more off
logit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)

margins, at((means) _all lag_ted_empper1=(.3668891 .4712813) remote=0 econaidtrsigned = 0 anoc_rs = 0 democ_rs = 1 xconst_p4_1 = 7 democratizing_rs =0  ////
polcondem = .3763111 samesys = 0)

*remote
set more off
logit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2  $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
margins, at((means) _all remote=(0 1) econaidtrsigned = 0 anoc_rs = 0 democ_rs = 1 xconst_p4_1 = 7 democratizing_rs =0  ////
polcondem = .3763111 samesys = 0)

*productivity
set more off
logit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
margins, at((means) _all lag_ln_ted_outhour1=(2.51788 3.605825) remote=0 econaidtrsigned = 0 anoc_rs = 0 democ_rs = 1 xconst_p4_1 = 7 democratizing_rs =0  ////
polcondem = .3763111 samesys = 0)


*Schooling
set more off
logit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
margins, at((means) _all yr_sch_25999_int_bl_2=(.904019 7.93025) remote=0 econaidtrsigned = 0 anoc_rs = 0 democ_rs = 1 xconst_p4_1 = 7 democratizing_rs =0  ////
polcondem = .3763111 samesys = 0)


*Employment growth
set more off
logit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
margins, at((means) _all ted_empgrow2=(.6544503 3.166293) remote=0 econaidtrsigned = 0 anoc_rs = 0 democ_rs = 1 xconst_p4_1 = 7 democratizing_rs =0  ////
polcondem = .3763111 samesys = 0)


****Robustness checks and Extensions

**Table A1
gen gdppc_ratio=ted_gdppc1/ted_gdppc2
label var gdppc_ratio "Ratio of GDP per capita (RS/SS)"


*GDPpc instead of GDP 
set more off
eststo clear
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth gdppc_ratio  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
set more off
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth gdppc_ratio ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)

*splines instead of cubic
set more off
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth  //// 
yrsincetr spl1 spl2 spl3  if yrunderorigin==0, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth  ////
$controls ////
yrsincetr spl1 spl2 spl3 if yrunderorigin==0, cluster(dyad)
esttab using tableA1.tex,label b(2) se(2)  star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
title("Robustness checks for logistic regressions testing the hypotheses" \label{tab:robust}) nogaps booktabs replace ///
compress order(lag_ted_empper1 remote75 lag_ln_ted_outhour1 yr_sch_25999_int_bl_2 ted_empgrow2 ted_ln_gdp1 ted_ln_gdp2 gdppc_ratio  ////
econaidtrsigned anoc_rs democ_rs democratizing_rs    polconiii polcondem xconst_p4_1 samesys mindistance_1000 yrsincetr yrsincetr2 yrsincetr3 ////
spl1 spl2 spl3 )


**Table A2


  gen treatyinforce=0
  replace treatyinforce=origin if origin==1
  replace treatyinforce=yrunderorigin if yrunderorigin==1

  sort dyadid year
by dyadid: gen treatyinforce_lag1=treatyinforce[_n-1]
label var treatyinforce_lag1 "Treaty in force"
label define tr2 0 "Treaty in force" 1 "Treaty in force"
label values treatyinforce_lag1 tr2
global controls_mtm i.econaidtrsigned##i.treatyinforce_lag1 i.anoc_rs##i.treatyinforce_lag1 i.democ_rs##i.treatyinforce_lag1 c.xconst_p4_1##i.treatyinforce_lag1 ////
 i.democratizing_rs##i.treatyinforce_lag1 ////
  c.polconiii##i.treatyinforce_lag1 c.polcondem##i.treatyinforce_lag1 i.samesys##i.treatyinforce_lag1 c.mindistance_1000##i.treatyinforce_lag1

  
set more off
eststo clear
eststo: probit treatyinforce c.lag_ted_empper1##i.treatyinforce_lag1 c.lag_ln_ted_outhour1##i.treatyinforce_lag1 i.remote75##i.treatyinforce_lag1 c.yr_sch_25999_int_bl_2##i.treatyinforce_lag1 ////
 c.ted_empgrow2##i.treatyinforce_lag1 c.ted_ln_gdp1##i.treatyinforce_lag1  c.ted_ln_gdp2##i.treatyinforce_lag1 , cluster(dyad)
set more off
 eststo: probit treatyinforce c.lag_ted_empper1##i.treatyinforce_lag1 c.lag_ln_ted_outhour1##i.treatyinforce_lag1 i.remote75##i.treatyinforce_lag1 c.yr_sch_25999_int_bl_2##i.treatyinforce_lag1 ////
 c.ted_empgrow2##i.treatyinforce_lag1 c.ted_ln_gdp1##i.treatyinforce_lag1  c.ted_ln_gdp2##i.treatyinforce_lag1 ////
$controls_mtm, cluster(dyad)
esttab using tableA2.tex,label b(2) se(2) wide star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
title("Markov transition model testing the hypotheses" \label{tab:markov}) nogaps booktabs replace ///
compress order(lag_ted_empper1 remote75 lag_ln_ted_outhour1 yr_sch_25999_int_bl_2 ted_empgrow2 ted_ln_gdp1 ted_ln_gdp2 ////
econaidtrsigned anoc_rs democ_rs democratizing_rs    polconiii polcondem xconst_p4_1 samesys mindistance_1000)
  
  
***Table A3  
set more off
eststo clear
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3, cluster(dyad)
esttab using tableA3.tex,label wide b(2) se(2)  star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
title("Logistic regressions testing the hypotheses without dropping years in which treaty is in force" \label{tab:nodropping}) nogaps booktabs replace ///
compress order(lag_ted_empper1 remote75 lag_ln_ted_outhour1 yr_sch_25999_int_bl_2 ted_empgrow2 ted_ln_gdp1 ted_ln_gdp2 ////
econaidtrsigned anoc_rs democ_rs democratizing_rs    polconiii polcondem xconst_p4_1 samesys mindistance_1000 yrsincetr yrsincetr2 yrsincetr3)
  

***Table A4 

label var remote90 "Remote (RS; 90\%)"
label var remote50 "Remote (RS; 50\%)"
gen meandist_1000=meandist/1000
gen meandist_1000_2=meandist_1000^2
label var meandist_1000 "Mean distance from all states (RS)"
label var meandist_1000_2 "Mean distance from all states$^{2}$ (RS)"


set more off
eststo clear
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote50 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote50 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)

eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote90 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote90 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)

eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 meandist_1000 meandist_1000_2 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 meandist_1000 meandist_1000_2 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)


esttab using tableA4.tex,label  b(2) se(2)  star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
title("Logistic regressions with different cut points for remoteness" \label{tab:remote}) nogaps booktabs replace ///
compress order(lag_ted_empper1 remote50 remote90 meandist_1000 meandist_1000_2 lag_ln_ted_outhour1 yr_sch_25999_int_bl_2 ted_empgrow2 ted_ln_gdp1 ted_ln_gdp2 ////
econaidtrsigned anoc_rs democ_rs democratizing_rs    polconiii polcondem xconst_p4_1 samesys mindistance_1000 yrsincetr yrsincetr2 yrsincetr3)
  

  
**Table A5
*Immigration policy
set more off
eststo clear
label var immipol "Immigration Policy (1yr lag)"
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 immipol $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 immipol $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)



*flow
sort dyad year
by dyad: gen l_flow=flow[_n-1]
gen l_ln_flow=ln(l_flow)
label var l_ln_flow "Dyadic Immigrant Flow (logged; 1 yr lag)"

sort dyad year
by dyad: gen l_istock=istock[_n-1]
gen l_ln_istock=ln(istock)
label var l_ln_istock "Dyadic Immigrant Stock (logged; 1 yr lag)"

eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 l_ln_flow $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 l_ln_flow $wealth ////
samesys mindistance_1000 ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)

eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 l_ln_istock $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 l_ln_istock $wealth ////
samesys mindistance_1000 ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)


esttab using tableA5.tex,label b(2) se(2)  star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
title("Logistic regressions examining extenstions to the model" \label{tab:extensions}) nogaps booktabs replace ///
compress order(lag_ted_empper1 remote75 lag_ln_ted_outhour1 yr_sch_25999_int_bl_2 ted_empgrow2 immipol l_ln_flow l_ln_istock ted_ln_gdp1 ted_ln_gdp2 ////
econaidtrsigned anoc_rs democ_rs democratizing_rs    polconiii polcondem xconst_p4_1 samesys mindistance_1000 yrsincetr yrsincetr2 yrsincetr3)
  
 
  
  

**Table A6
set more off
eststo clear
eststo: relogit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
eststo: logit origin lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderorigin==0, cluster(dyad)
esttab using tableA6.tex,label b(2) se(2) wide star(+ 0.10 * 0.05 ** 0.01 *** 0.001) booktabs ///
title("Logistic regressions testing the hypotheses using different Logistic Models" \label{tab:diflogits}) nogaps replace ///
compress order(lag_ted_empper1 remote75 lag_ln_ted_outhour1 yr_sch_25999_int_bl_2 ted_empgrow2 ted_ln_gdp1 ted_ln_gdp2 ////
econaidtrsigned anoc_rs democ_rs democratizing_rs    polconiii polcondem xconst_p4_1 samesys mindistance_1000 yrsincetr yrsincetr2 yrsincetr3) ////
mtitles ("Rare Events Logit" "Logit")
  


**Table A7
set more off
eststo clear
eststo: relogit alltreaties lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth  //// 
yrsincetr yrsincetr2 yrsincetr3  if yrunderboth==0, cluster(dyad)
eststo: relogit alltreaties lag_ted_empper1 lag_ln_ted_outhour1 remote75 yr_sch_25999_int_bl_2 ted_empgrow2 $wealth ////
$controls ////
yrsincetr yrsincetr2 yrsincetr3  if yrunderboth==0, cluster(dyad)
esttab using tableA7.tex,label wide b(2) se(2)  star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
title("Logistic regressions testing the hypotheses on both originating and amending/ supplemental treaties" \label{tab:alltreaties}) nogaps booktabs replace ///
compress order(lag_ted_empper1 remote75 lag_ln_ted_outhour1 yr_sch_25999_int_bl_2 ted_empgrow2 ted_ln_gdp1 ted_ln_gdp2 ////
econaidtrsigned anoc_rs democ_rs democratizing_rs    polconiii polcondem xconst_p4_1 samesys mindistance_1000 yrsincetr yrsincetr2 yrsincetr3)

